package cn.edu.scau.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.entity.Customer;
import cn.edu.scau.entity.GoOutRegister;
import cn.edu.scau.entity.LiveInRegister;
import cn.edu.scau.service.BedService;
import cn.edu.scau.service.CustomerService;
import cn.edu.scau.service.GoOutRegisterService;
import cn.edu.scau.service.LiveInRegisterService;
import cn.edu.scau.service.ManagerService;
import cn.edu.scau.service.NurseService;
import io.swagger.annotations.Api;

/**
 * customer 模块控制器
 * @author LUSING
 *
 */
@RequestMapping(value = "customers")
@RestController
@Api(tags = "客户信息控制器")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	@Autowired
	private GoOutRegisterService goOutRegisterService;
	@Autowired
	private LiveInRegisterService liveInRegisterService;
	@Autowired
	private BedService bedService;
	@Autowired 
	private NurseService nurseService;
	@Autowired
	private ManagerService managerService;
	
	@GetMapping(value = "list/{nid}")
	public ResultBean list(@PathVariable("nid")Integer nid) {
		ResultBean r = null;
		List<Customer> customers = null;
		if(0 == nid) {
			customers = customerService.selectAll();
		}
		else {
			customers = customerService.selectByNurseId(nid);
		}
		r = new ResultBean(200,true,"获取客户列表成功",customers);		
		return r;
	}
	/**
	 * 根据客户id、名称模糊查询
	 * @param customerid
	 * @param customername
	 * @return
	 */
	@GetMapping(value = "find")
	public ResultBean find(Integer customerid,String customername) {
		ResultBean r = null;
			List<Customer> customers = customerService.find(customerid, customername);
			r = new ResultBean(200,true,"获取客户列表成功",customers);	
		
		return r;
	}
	
	/**
	 * 获取客户信息以便存于localstorage
	 * @param id
	 * @return
	 */
	@GetMapping(value = "detail/{id}")
	public ResultBean detail(@PathVariable("id")Integer id) {
		ResultBean r = null;
		Customer customer = customerService.selectByPrimaryKey(id);
		r = new ResultBean(200,true,"获取客户列表成功",customer);
		return r;
	}
	/**
	 * 根据客户id获取客户外出记录，参数为0时列出所有数据
	 * @param cid
	 * @return
	 */
	@GetMapping(value = "getGoout/{cid}")
	public ResultBean listGoout(@PathVariable("cid")Integer cid) {
		ResultBean r = null;
		List<GoOutRegister> goOutRegisters = null;
		if(0 == cid)
			goOutRegisters = goOutRegisterService.selectAll();
		else
			goOutRegisters = goOutRegisterService.selectByCustomerId(cid);
		r = new ResultBean(200,true,"获取外出记录列表成功",goOutRegisters);
		return r;
	}
	/**
	 * 根据返回的主键查询外出记录，并修改实际返回时间
	 * @return
	 */
	@PutMapping(value = "getback")
	public ResultBean getBack(Date gooutdate,Date expectedbackdate) {
		ResultBean r = null;
		boolean record = goOutRegisterService.updateGoOutRegisterByPrimaryKey(gooutdate, expectedbackdate);
		if(record)
			r = new ResultBean(200,true,"确认返院成功",record);
		else
			r = new ResultBean(500,false,"确认返院失败",record);
		return r;
	}
	/**
	 * 新增外出记录
	 * @param record
	 * @return
	 */
	@PostMapping(value = "goout")
	public ResultBean goout(GoOutRegister record) {
		ResultBean r = null;
		boolean result = goOutRegisterService.insert(record);
		if(result)
			r = new ResultBean(200,true,"新增外出记录成功",record);
		else
			r = new ResultBean(500,false,"新增外出记录失败",record);
		return r;
	}
	/**
	 * 删除客户（确认离院，将flag改为0，修改入住登记表中对应数据）
	 * @param id
	 * @return
	 */
	@PutMapping(value = "left/{id}")
	public ResultBean left(@PathVariable("id")Integer id) {
		ResultBean r = null;
		boolean result = customerService.safelyDeleteCustomer(id);
		if(result)
			r = new ResultBean(200,true,"确认离院成功",id);
		else
			r = new ResultBean(500,false,"确认离院失败",id);
		return r;
	}
	/**
	 * 根据护士id列出入住记录列表
	 * @param nid
	 * @return
	 */
	@GetMapping(value = "listLivein/{nid}")
	public ResultBean listLivein(@PathVariable("nid")Integer nid) {
		ResultBean r = null;
		List<LiveInRegister> records = null;
		if(0!=nid)
			records = liveInRegisterService.selectByNurseID(nid);
		else 
			records = liveInRegisterService.selectAll();
		
		r = new ResultBean(200,true,"获取入住记录成功",records);
		return r;
	}
	/**
	 * 根据客户id、名称组合模糊查询获取入住记录
	 * @param customerid
	 * @param customername
	 * @return
	 */
	@GetMapping(value = "findLivein")
	public ResultBean findLivein(Integer customerid,String customername) {
		ResultBean r = null;
		List<LiveInRegister> records = liveInRegisterService.findByIdName(customerid, customername);
		r = new ResultBean(200,true,"获取入住记录成功",records);
		return r;
	}
	
	/**
	 * 新增入住记录
	 * @return
	 */
	@PostMapping(value = "livein")
	public ResultBean insertLivein(LiveInRegister record,BigDecimal bookingprepayamount) {
		ResultBean r = null;
		//检测customerid是否存在，若不存在则需要新增用户
		//生成新的booking
		//检测床位是否可用(以上功能从前端完成)
		liveInRegisterService.insert(record,bookingprepayamount);
		r = new ResultBean(200,true,"新增入住记录",record);
		return r;
	}
	
	/**
	 * 检查客户是否存在并返回至前端
	 * @param customerid
	 * @return
	 */
	@GetMapping(value = "checkCustomer")
	public ResultBean checkCustomer(Integer customerid) {
		ResultBean r=null;
		if(null== customerService.selectByPrimaryKey(customerid))
			r = new ResultBean(500,false,"不存在客户",null);
		else 
			r = new ResultBean(200,true,"存在客户",null);
		return r;
	}
	
	/**
	 * 检查床位是否可用并返回至前端
	 * @param bedid
	 * @return
	 */
	@GetMapping(value = "checkBed")
	public ResultBean checkBed(Integer bedid) {
		ResultBean r=null;
		if(bedService.checkOccupiedById(bedid))
			r = new ResultBean(500,false,"床位已被占用",null);
		else 
			r = new ResultBean(200,true,"床位可用",null);
		return r;
	}
	
	/**
	 * 新增客户
	 * @param record
	 * @return
	 */
	@PostMapping(value = "newCustomer")
	public ResultBean newCustomer(Customer record) {
		ResultBean r = null;
		record.setCustomeruseflag(true);
		Integer id = customerService.insert(record);
		if(null!=id)
			r = new ResultBean(200,true,"新增客户成功",id);
		else
			r = new ResultBean(500,false,"新增客户失败",id);
		return r;
	}
	/**
	 * 根据useraccount 读取nurseid
	 * @param userid
	 * @return
	 */
	@GetMapping(value = "getnid")
	public ResultBean getNurseID(String useraccount) {
		ResultBean r = null;
		Integer userid = managerService.selectByUserAccount(useraccount).getUserId();
		Integer id = nurseService.selectByUserId(userid).getNurseid();
		if(null!=id)
			r = new ResultBean(200,true,"读取成功",id);
		else
			r = new ResultBean(500,false,"读取失败",id);
		return r;
	}
	/**
	 * 请求床位信息
	 * @return
	 */
	@GetMapping(value = "listBed")
	public ResultBean listBed() {
		return new ResultBean(200,true,"获取床位成功",bedService.selectAll());
	}
	
	/**
	 * 修改床位描述
	 * @param bedid
	 * @param bedDescription
	 * @return
	 */
	@PutMapping(value = "updateBed")
	public ResultBean updateBed(Integer bedid,String bedDescription) {
		ResultBean r=null;
		if(bedService.updateBedDescription(bedid, bedDescription))
			r = new ResultBean(200,true,"修改成功",null);
		else 
			r = new ResultBean(500,false,"修改失败",null);
		return r;
	}
}
